﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_ProductsDAL : IDcms_ProductsDAL
    {
        public Dcms_ProductsModel AddModel(Dcms_ProductsModel model)
        {
            string sQLString = "insert into Dcms_Products(Products_CateID,Products_Name,Products_CodeName,Products_BigImage,Products_MinImage,Products_FileIntro,Products_KeyWord,Products_Target,Products_Introduction,Products_OtherInfo,Products_Count,Products_Order,Products_State,Products_AddTime,Products_IsHot,Products_IsNew,Products_Prices,Products_ExFlag1,Products_ExFlag2,Products_ExFlag3,Products_ExFlag4,Products_ExFlag5,Products_ExFlag6,Products_ExFlag7,Products_ExFlag8,Products_ExFlag9,Products_ExFlag10) values(@Products_CateID,@Products_Name,@Products_CodeName,@Products_BigImage,@Products_MinImage,@Products_FileIntro,@Products_KeyWord,@Products_Target,@Products_Introduction,@Products_OtherInfo,@Products_Count,@Products_Order,@Products_State,@Products_AddTime,@Products_IsHot,@Products_IsNew,@Products_Prices,@Products_ExFlag1,@Products_ExFlag2,@Products_ExFlag3,@Products_ExFlag4,@Products_ExFlag5,@Products_ExFlag6,@Products_ExFlag7,@Products_ExFlag8,@Products_ExFlag9,@Products_ExFlag10) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Products_CateID", SqlDbType.Int), new SqlParameter("@Products_Name", SqlDbType.VarChar), new SqlParameter("@Products_CodeName", SqlDbType.VarChar), new SqlParameter("@Products_BigImage", SqlDbType.VarChar), new SqlParameter("@Products_MinImage", SqlDbType.VarChar), new SqlParameter("@Products_FileIntro", SqlDbType.VarChar), new SqlParameter("@Products_KeyWord", SqlDbType.VarChar), new SqlParameter("@Products_Target", SqlDbType.VarChar), new SqlParameter("@Products_Introduction", SqlDbType.Text), new SqlParameter("@Products_OtherInfo", SqlDbType.Text), new SqlParameter("@Products_Count", SqlDbType.Int), new SqlParameter("@Products_Order", SqlDbType.Int), new SqlParameter("@Products_State", SqlDbType.VarChar), new SqlParameter("@Products_AddTime", SqlDbType.DateTime), new SqlParameter("@Products_IsHot", SqlDbType.VarChar), 
                new SqlParameter("@Products_IsNew", SqlDbType.VarChar), new SqlParameter("@Products_Prices", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag2", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag3", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag4", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag5", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag10", SqlDbType.VarChar)
             };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Products_CateID;
            cmdParms[2].Value = model.Products_Name;
            cmdParms[3].Value = model.Products_CodeName;
            cmdParms[4].Value = model.Products_BigImage;
            cmdParms[5].Value = model.Products_MinImage;
            cmdParms[6].Value = model.Products_FileIntro;
            cmdParms[7].Value = model.Products_KeyWord;
            cmdParms[8].Value = model.Products_Target;
            cmdParms[9].Value = model.Products_Introduction;
            cmdParms[10].Value = model.Products_OtherInfo;
            cmdParms[11].Value = model.Products_Count;
            cmdParms[12].Value = model.Products_Order;
            cmdParms[13].Value = model.Products_State;
            cmdParms[14].Value = model.Products_AddTime;
            cmdParms[15].Value = model.Products_IsHot;
            cmdParms[0x10].Value = model.Products_IsNew;
            cmdParms[0x11].Value = model.Products_Prices;
            cmdParms[0x12].Value = model.Products_ExFlag1;
            cmdParms[0x13].Value = model.Products_ExFlag2;
            cmdParms[20].Value = model.Products_ExFlag3;
            cmdParms[0x15].Value = model.Products_ExFlag4;
            cmdParms[0x16].Value = model.Products_ExFlag5;
            cmdParms[0x17].Value = model.Products_ExFlag6;
            cmdParms[0x18].Value = model.Products_ExFlag7;
            cmdParms[0x19].Value = model.Products_ExFlag8;
            cmdParms[0x1a].Value = model.Products_ExFlag9;
            cmdParms[0x1b].Value = model.Products_ExFlag10;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Products_ID = (int) cmdParms[0].Value;
            return model;
        }

        public bool CopyProducts(int SourceCateID, int DestCateID)
        {
            string sQLString = "insert into Dcms_Products (Products_CateID, Products_Name, Products_CodeName, Products_BigImage, Products_MinImage, Products_FileIntro, Products_KeyWord, Products_Target, Products_Introduction, Products_OtherInfo, Products_Count, Products_Order, Products_State, Products_AddTime, Products_IsHot, Products_IsNew, Products_Prices, Products_ExFlag1, Products_ExFlag2, Products_ExFlag3, Products_ExFlag4, Products_ExFlag5, Products_ExFlag6, Products_ExFlag7, Products_ExFlag8, Products_ExFlag9, Products_ExFlag10)";
            sQLString = sQLString + "select @DestCateID, Products_Name, Products_CodeName, Products_BigImage, Products_MinImage, Products_FileIntro, Products_KeyWord, Products_Target, Products_Introduction, Products_OtherInfo, Products_Count, Products_Order, Products_State, Products_AddTime, Products_IsHot, Products_IsNew, Products_Prices, Products_ExFlag1, Products_ExFlag2, Products_ExFlag3, Products_ExFlag4, Products_ExFlag5, Products_ExFlag6, Products_ExFlag7, Products_ExFlag8, Products_ExFlag9, Products_ExFlag10 from Dcms_Products where Products_CateID=@SourceCateID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SourceCateID", SqlDbType.Int), new SqlParameter("@DestCateID", SqlDbType.Int) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return SqlHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public IList<Dcms_ProductsModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_ProductsModel> list = new List<Dcms_ProductsModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_ProductsModel item = new Dcms_ProductsModel();
                if (ds.Tables[0].Rows[i]["Products_ID"].ToString() != "") item.Products_ID = int.Parse(ds.Tables[0].Rows[i]["Products_ID"].ToString());
                if (ds.Tables[0].Rows[i]["Products_CateID"].ToString() != "") item.Products_CateID = int.Parse(ds.Tables[0].Rows[i]["Products_CateID"].ToString());
                item.Products_Name = ds.Tables[0].Rows[i]["Products_Name"].ToString();
                item.Products_CodeName = ds.Tables[0].Rows[i]["Products_CodeName"].ToString();
                item.Products_BigImage = ds.Tables[0].Rows[i]["Products_BigImage"].ToString();
                item.Products_MinImage = ds.Tables[0].Rows[i]["Products_MinImage"].ToString();
                item.Products_FileIntro = ds.Tables[0].Rows[i]["Products_FileIntro"].ToString();
                item.Products_KeyWord = ds.Tables[0].Rows[i]["Products_KeyWord"].ToString();
                item.Products_Target = ds.Tables[0].Rows[i]["Products_Target"].ToString();
                item.Products_Introduction = ds.Tables[0].Rows[i]["Products_Introduction"].ToString();
                item.Products_OtherInfo = ds.Tables[0].Rows[i]["Products_OtherInfo"].ToString();
                if (ds.Tables[0].Rows[i]["Products_Count"].ToString() != "") item.Products_Count = int.Parse(ds.Tables[0].Rows[i]["Products_Count"].ToString());
                if (ds.Tables[0].Rows[i]["Products_Order"].ToString() != "") item.Products_Order = int.Parse(ds.Tables[0].Rows[i]["Products_Order"].ToString());
                item.Products_State = ds.Tables[0].Rows[i]["Products_State"].ToString();
                if (ds.Tables[0].Rows[i]["Products_AddTime"].ToString() != "") item.Products_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Products_AddTime"].ToString());
                item.Products_IsHot = ds.Tables[0].Rows[i]["Products_IsHot"].ToString();
                item.Products_IsNew = ds.Tables[0].Rows[i]["Products_IsNew"].ToString();
                item.Products_Prices = ds.Tables[0].Rows[i]["Products_Prices"].ToString();
                item.Products_ExFlag1 = ds.Tables[0].Rows[i]["Products_ExFlag1"].ToString();
                item.Products_ExFlag2 = ds.Tables[0].Rows[i]["Products_ExFlag2"].ToString();
                item.Products_ExFlag3 = ds.Tables[0].Rows[i]["Products_ExFlag3"].ToString();
                item.Products_ExFlag4 = ds.Tables[0].Rows[i]["Products_ExFlag4"].ToString();
                item.Products_ExFlag5 = ds.Tables[0].Rows[i]["Products_ExFlag5"].ToString();
                item.Products_ExFlag6 = ds.Tables[0].Rows[i]["Products_ExFlag6"].ToString();
                item.Products_ExFlag7 = ds.Tables[0].Rows[i]["Products_ExFlag7"].ToString();
                item.Products_ExFlag8 = ds.Tables[0].Rows[i]["Products_ExFlag8"].ToString();
                item.Products_ExFlag9 = ds.Tables[0].Rows[i]["Products_ExFlag9"].ToString();
                item.Products_ExFlag10 = ds.Tables[0].Rows[i]["Products_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return SqlHelper.ExecuteSql("delete from Dcms_Products where Products_ID In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Products where Products_ID=@Products_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Products_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_ProductsModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_ProductsModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Products where Products_ID=@Products_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Products_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_ProductsModel model = new Dcms_ProductsModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ProductsModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Products";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_ProductsModel GetNewModelByCatID(int CatID)
        {
            return null;
        }

        public bool MoveProducts(int SourceCateID, int DestCateID)
        {
            string sQLString = "update Dcms_Products set Products_CateID=@DestCateID where Products_CateID=@SourceCateID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SourceCateID", SqlDbType.Int), new SqlParameter("@DestCateID", SqlDbType.Int) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return SqlHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public Dcms_ProductsModel Update(Dcms_ProductsModel model)
        {
            string sQLString = "update Dcms_Products set Products_CateID=@Products_CateID,Products_Name=@Products_Name,Products_CodeName=@Products_CodeName,Products_BigImage=@Products_BigImage,Products_MinImage=@Products_MinImage,Products_FileIntro=@Products_FileIntro,Products_KeyWord=@Products_KeyWord,Products_Target=@Products_Target,Products_Introduction=@Products_Introduction,Products_OtherInfo=@Products_OtherInfo,Products_Count=@Products_Count,Products_Order=@Products_Order,Products_State=@Products_State,Products_AddTime=@Products_AddTime,Products_IsHot=@Products_IsHot,Products_IsNew=@Products_IsNew,Products_Prices=@Products_Prices,Products_ExFlag1=@Products_ExFlag1,Products_ExFlag2=@Products_ExFlag2,Products_ExFlag3=@Products_ExFlag3,Products_ExFlag4=@Products_ExFlag4,Products_ExFlag5=@Products_ExFlag5,Products_ExFlag6=@Products_ExFlag6,Products_ExFlag7=@Products_ExFlag7,Products_ExFlag8=@Products_ExFlag8,Products_ExFlag9=@Products_ExFlag9,Products_ExFlag10=@Products_ExFlag10 where Products_ID=@Products_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@Products_CateID", SqlDbType.Int), new SqlParameter("@Products_Name", SqlDbType.VarChar), new SqlParameter("@Products_CodeName", SqlDbType.VarChar), new SqlParameter("@Products_BigImage", SqlDbType.VarChar), new SqlParameter("@Products_MinImage", SqlDbType.VarChar), new SqlParameter("@Products_FileIntro", SqlDbType.VarChar), new SqlParameter("@Products_KeyWord", SqlDbType.VarChar), new SqlParameter("@Products_Target", SqlDbType.VarChar), new SqlParameter("@Products_Introduction", SqlDbType.Text), new SqlParameter("@Products_OtherInfo", SqlDbType.Text), new SqlParameter("@Products_Count", SqlDbType.Int), new SqlParameter("@Products_Order", SqlDbType.Int), new SqlParameter("@Products_State", SqlDbType.VarChar), new SqlParameter("@Products_AddTime", SqlDbType.DateTime), new SqlParameter("@Products_IsHot", SqlDbType.VarChar), new SqlParameter("@Products_IsNew", SqlDbType.VarChar), 
                new SqlParameter("@Products_Prices", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag2", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag3", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag4", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag5", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Products_ExFlag10", SqlDbType.VarChar), new SqlParameter("@Products_ID", SqlDbType.Int)
             };
            cmdParms[0].Value = model.Products_CateID;
            cmdParms[1].Value = model.Products_Name;
            cmdParms[2].Value = model.Products_CodeName;
            cmdParms[3].Value = model.Products_BigImage;
            cmdParms[4].Value = model.Products_MinImage;
            cmdParms[5].Value = model.Products_FileIntro;
            cmdParms[6].Value = model.Products_KeyWord;
            cmdParms[7].Value = model.Products_Target;
            cmdParms[8].Value = model.Products_Introduction;
            cmdParms[9].Value = model.Products_OtherInfo;
            cmdParms[10].Value = model.Products_Count;
            cmdParms[11].Value = model.Products_Order;
            cmdParms[12].Value = model.Products_State;
            cmdParms[13].Value = model.Products_AddTime;
            cmdParms[14].Value = model.Products_IsHot;
            cmdParms[15].Value = model.Products_IsNew;
            cmdParms[0x10].Value = model.Products_Prices;
            cmdParms[0x11].Value = model.Products_ExFlag1;
            cmdParms[0x12].Value = model.Products_ExFlag2;
            cmdParms[0x13].Value = model.Products_ExFlag3;
            cmdParms[20].Value = model.Products_ExFlag4;
            cmdParms[0x15].Value = model.Products_ExFlag5;
            cmdParms[0x16].Value = model.Products_ExFlag6;
            cmdParms[0x17].Value = model.Products_ExFlag7;
            cmdParms[0x18].Value = model.Products_ExFlag8;
            cmdParms[0x19].Value = model.Products_ExFlag9;
            cmdParms[0x1a].Value = model.Products_ExFlag10;
            cmdParms[0x1b].Value = model.Products_ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public void UpDateCount(int ID)
        {
            string sQLString = "Update Dcms_Products Set Products_Count=(Products_Count+1) where Products_ID=@Products_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Products_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return SqlHelper.ExecuteSql("Update Dcms_Products Set Products_State='" + StateValue.ToString() + "' where Products_ID In (" + AllID + ")", null) > 0;
        }
    }
}

